using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using ClientGUI.Widgets;

using OWED.Network;
using OWED.Network.Messages;
using OWED.Network.Messages.Reply;
using OWED.Network.Messages.General;

namespace ClientGUI.Main.PopUpWindows
{
    public partial class ApproveProposal : NetworkDialog
    {
        MainForm my_main;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="main">parent</param>
        public ApproveProposal(MainForm main): base("Approve Proposal:", main)
        {
            InitializeComponent();
            my_main = main;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void ApproveProposal_Load(object sender, EventArgs e)
        {
            Show();
            proposal_list();
        }


        private void proposal_list()
        {
            ListProposals ls_prop = new ListProposals(my_main.ProgramData.User,
                                                        my_main.ProgramData.DocName);
            XmlDocument re = my_main.ProgramData.Connection.Send(
                ls_prop.ToXml());
            if (re != null)
            {
                ProposalListReply reply = 
                     MessageGet.GetFromXml(re) as ProposalListReply;
                if (reply != null && reply.Proposals.Count > 0)
                {
                    foreach (Proposal prop in reply.Proposals)
                    {
                        String[] columns = new String[2];
                        columns[0] = prop.ProposalID.number.ToString();
                        String date = prop.Proposed.Date.year + " " +
                                        prop.Proposed.Date.month + " " +
                                        prop.Proposed.Date.day + " at " +
                                        prop.Proposed.Date.hour + ":" + 
                                        prop.Proposed.Date.minute + ":" + 
                                        prop.Proposed.Date.second;
                        columns[1] = date;
                        listView1.Items.Add(new ListViewItem(columns));
                    }
                    StatusMessage("Proposals Listed");
                }
                else if (reply == null)
                {
                    StatusMessage("Backend gave an invalid response");
                }
                else
                {
                    StatusMessage("No proposals on this document version");
                }
            }
            else
            {
                StatusMessage("Failed to list proposals");
            }
        }
    }
}